package cn.pink.dbsrv;

import cn.pink.core.gen.proxy.RPCProxyBase;
import cn.pink.core.*;
import cn.pink.common.support.tools.node.NodeTool;
import cn.pink.core.support.Param;
import cn.pink.core.support.Utils;
import cn.pink.core.support.function.*;
import java.util.Map;
import java.util.Set;
import cn.pink.core.gen.IronGenFile;

@SuppressWarnings("rawtypes")
@IronGenFile
public class RPCProxy {
	public static final class DBServiceProxy extends RPCProxyBase {
		public final class EnumCall{
			public static final int CN_PINK_DBSRV_DBSERVICE_CACHEPERSIST_STRING_STRING_BYTES = 1;
			public static final int CN_PINK_DBSRV_DBSERVICE_DEFAULTHANDLER_STRING = 2;
			public static final int CN_PINK_DBSRV_DBSERVICE_UPDATE_STRING_MAP_SET = 3;
			public static final int CN_PINK_DBSRV_DBSERVICE_CACHELOAD_STRING = 4;
			public static final int CN_PINK_DBSRV_DBSERVICE_FINDHUMAN_STRING = 5;
			public static final int CN_PINK_DBSRV_DBSERVICE_IDALLOT_LONG = 6;
		}

		public static final String DIST_NAME = "DBService";

		private CallPoint remote;
		private Port localPort;

		private static NodeType nodeType = NodeType.DB;
		
		/**
		 * 私有构造函数
		 * 防止实例被私自创建 必须通过newInstance函数
		 */
		private DBServiceProxy() {}
	
		/**
		 * 伪随机随机获取远程实例
		 * 优先获取本地实例
		 */
		public static DBServiceProxy newInstance() {
			NodeInfo nodeInfo = NodeTool.getInstance().getRandomNode(nodeType);
			return createInstance(nodeInfo.getNodeId(), nodeInfo.getPortId(), DIST_NAME);
		}

		public static DBServiceProxy newInstance(String nodeId, String portId) {
			return createInstance(nodeId, portId, DIST_NAME);
		}

		/**
		 * 创建实例
		 */
		private static DBServiceProxy createInstance(String node, String port, Object id) {
			DBServiceProxy inst = new DBServiceProxy();
			inst.localPort = Port.getCurrent();
			inst.remote = new CallPoint(node, port, id);
			
			return inst;
		}
		
		/**
		 * 监听返回值
		 */
		public void listenResult(IronFunction2<Param, Param> method, Object...context) {
			listenResult(method, new Param(context));
		}
		
		/**
		 * 监听返回值
		 */
		public void listenResult(IronFunction2<Param, Param> method, Param context) {
			context.put("_callerInfo", remote.callerInfo);
			localPort.listenResult(method, context);
		}
		
		
		public void listenResult(IronFunction3<Boolean, Param, Param> method, Object...context) {
			listenResult(method, new Param(context));
		}
		
		public void listenResult(IronFunction3<Boolean, Param, Param> method, Param context) {
			context.put("_callerInfo", remote.callerInfo);
			localPort.listenResult(method, context);
		}
		
		public void cachePersist(String arg0, String arg1, byte... arg2) {
			remote.callerInfo = Utils.getCallerInfo();
			localPort.call(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_CACHEPERSIST_STRING_STRING_BYTES, new Object[]{ arg0, arg1, arg2 });
		}
		public void defaultHandler(String arg0) {
			remote.callerInfo = Utils.getCallerInfo();
			localPort.call(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_DEFAULTHANDLER_STRING, new Object[]{ arg0 });
		}
		public void update(String arg0, Map arg1, Set arg2) {
			remote.callerInfo = Utils.getCallerInfo();
			localPort.call(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_UPDATE_STRING_MAP_SET, new Object[]{ arg0, arg1, arg2 });
		}
		public void cacheLoad(String arg0) {
			remote.callerInfo = Utils.getCallerInfo();
			localPort.call(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_CACHELOAD_STRING, new Object[]{ arg0 });
		}
		public void findHuman(String arg0) {
			remote.callerInfo = Utils.getCallerInfo();
			localPort.call(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_FINDHUMAN_STRING, new Object[]{ arg0 });
		}
		public void idAllot(long arg0) {
			remote.callerInfo = Utils.getCallerInfo();
			localPort.call(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_IDALLOT_LONG, new Object[]{ arg0 });
		}
		
		public Call makeCall_cachePersist(String arg0, String arg1, byte... arg2) {
			Call call = localPort.makeCall(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_CACHEPERSIST_STRING_STRING_BYTES, new Object[]{ arg0, arg1, arg2 });
			call.to.callerInfo = Utils.getCallerInfo();
			
			return call;
		}	
		public Call makeCall_defaultHandler(String arg0) {
			Call call = localPort.makeCall(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_DEFAULTHANDLER_STRING, new Object[]{ arg0 });
			call.to.callerInfo = Utils.getCallerInfo();
			
			return call;
		}	
		public Call makeCall_update(String arg0, Map arg1, Set arg2) {
			Call call = localPort.makeCall(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_UPDATE_STRING_MAP_SET, new Object[]{ arg0, arg1, arg2 });
			call.to.callerInfo = Utils.getCallerInfo();
			
			return call;
		}	
		public Call makeCall_cacheLoad(String arg0) {
			Call call = localPort.makeCall(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_CACHELOAD_STRING, new Object[]{ arg0 });
			call.to.callerInfo = Utils.getCallerInfo();
			
			return call;
		}	
		public Call makeCall_findHuman(String arg0) {
			Call call = localPort.makeCall(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_FINDHUMAN_STRING, new Object[]{ arg0 });
			call.to.callerInfo = Utils.getCallerInfo();
			
			return call;
		}	
		public Call makeCall_idAllot(long arg0) {
			Call call = localPort.makeCall(remote, EnumCall.CN_PINK_DBSRV_DBSERVICE_IDALLOT_LONG, new Object[]{ arg0 });
			call.to.callerInfo = Utils.getCallerInfo();
			
			return call;
		}	
	}
	
}